﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Pr0ject0.Models;

namespace Pr0ject0.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(LoginModel model)
        {
            string role;
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, true);
                    role = Roles.GetRolesForUser(model.UserName)[0];

                    if (role == "Hiring Manager")
                    {
                        return RedirectToAction("index", "hiringmanager");
                    }else if (role == "Hiring Specialist")
                    {
                        return RedirectToAction("index", "hiringspecialist");
                    }else if (role == "Staffing Expert")
                    {
                        return RedirectToAction("index", "staffingexpert");
                    }
                    else if (role == "Kiosk")
                    {
                        return RedirectToAction("index", "jobapplication");
                    }
                }else
                {
                    ModelState.AddModelError("", "Invalid username or password");
                }

            }

            

            return View();
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return Redirect("/Login/Index");
        }

    }
}
